|
1
|
|
|
/* |
|
2
|
|
|
* from-bytes: convert bytes to numbers and strings. |
|
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
|
4
|
|
|
* https://github.com/rochars/byte-data |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
const pad = require("../src/byte-padding.js"); |
|
|
|
|
|
|
8
|
|
|
const endianness = require("endianness"); |
|
9
|
|
|
const reader = require("../src/read-bytes.js"); |
|
10
|
|
|
const bitDepths = require("../src/bit-depth.js"); |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
|
14
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
15
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
|
16
|
|
|
* @param {Object} params The options. They are: |
|
17
|
|
|
* - "signed", defaults to false |
|
18
|
|
|
* - "float", defaults to false, true for floats. |
|
19
|
|
|
* float is available for 16, 32 and 64 bit depths. |
|
20
|
|
|
* - "base", defaults to 10, can be 2, 10 or 16 |
|
21
|
|
|
* - "char", defaults to false, true for strings |
|
22
|
|
|
* - "be", defaults to false, true for big endian |
|
23
|
|
|
* @return {!Array<number>|string} The values represented in the bytes. |
|
24
|
|
|
*/ |
|
25
|
|
|
function fromBytes(bytes, bitDepth, params={}) { |
|
26
|
|
|
let base = 10; |
|
27
|
|
|
if ("base" in params) { |
|
28
|
|
|
base = params.base; |
|
29
|
|
|
} |
|
30
|
|
|
if (params.be) { |
|
31
|
|
|
endianness.endianness(bytes, bitDepth / 8); |
|
32
|
|
|
} |
|
33
|
|
|
return readBytes( |
|
34
|
|
|
bytes, |
|
35
|
|
|
bitDepth, |
|
36
|
|
|
params.char, |
|
37
|
|
|
params.signed, |
|
38
|
|
|
params.float, |
|
39
|
|
|
base); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
|
44
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
|
45
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
|
46
|
|
|
* @param {boolean} isChar True if it is a string. |
|
47
|
|
|
* @param {boolean} isSigned True if the values should be signed. |
|
48
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
|
49
|
|
|
* @param {number} base The base, one of 2, 10 or 16. |
|
50
|
|
|
* @return {!Array<number>|string} The values represented in the bytes. |
|
51
|
|
|
*/ |
|
52
|
|
|
function readBytes(bytes, bitDepth, isChar, isSigned, isFloat, base) { |
|
53
|
|
|
let numbers = []; |
|
54
|
|
|
let i = 0; |
|
55
|
|
|
let j = 0; |
|
56
|
|
|
let offset = bitDepths.bitDepthOffsets[bitDepth]; |
|
57
|
|
|
let len = bytes.length - (offset -1); |
|
58
|
|
|
let maxBitDepthValue = bitDepths.maxBitDepth[bitDepth]; |
|
59
|
|
|
bytesToInt(bytes, base); |
|
60
|
|
|
let bitReader = getBitReader(bitDepth, isFloat, isChar); |
|
61
|
|
|
let signFunction = isSigned ? signed : function(x,y){return x;}; |
|
|
|
|
|
|
62
|
|
|
while (i < len) { |
|
63
|
|
|
numbers[j] = signFunction(bitReader(bytes, i), maxBitDepthValue); |
|
64
|
|
|
i += offset; |
|
65
|
|
|
j++; |
|
66
|
|
|
} |
|
67
|
|
|
if (isChar) { |
|
68
|
|
|
numbers = numbers.join(""); |
|
69
|
|
|
} |
|
70
|
|
|
return numbers; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Return a function to read binary data. |
|
75
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
|
76
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
|
77
|
|
|
* @param {boolean} isChar True if it is a string. |
|
78
|
|
|
* @return {Function} |
|
79
|
|
|
*/ |
|
80
|
|
|
function getBitReader(bitDepth, isFloat, isChar) { |
|
81
|
|
|
let readBitDepth = bitDepth; |
|
82
|
|
|
if (bitDepth == 2 || bitDepth == 4) { |
|
83
|
|
|
readBitDepth = 8; |
|
84
|
|
|
} |
|
85
|
|
|
if (isChar) { |
|
86
|
|
|
return reader.readChar; |
|
87
|
|
|
} else { |
|
88
|
|
|
return reader['read' + readBitDepth + 'Bit' + (isFloat ? "Float" : "")]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Turn bytes to base 10. |
|
94
|
|
|
* @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings. |
|
95
|
|
|
* @param {number} base The base. |
|
96
|
|
|
*/ |
|
97
|
|
|
function bytesToInt(bytes, base) { |
|
98
|
|
|
if (base != 10) { |
|
99
|
|
|
let i = 0; |
|
100
|
|
|
let len = bytes.length; |
|
101
|
|
|
while(i < len) { |
|
102
|
|
|
bytes[i] = parseInt(bytes[i], base); |
|
103
|
|
|
i++; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Turn a unsigned number to a signed number. |
|
110
|
|
|
* @param {number} number The number. |
|
111
|
|
|
* @param {number} maxValue The max range for the number bit depth. |
|
112
|
|
|
*/ |
|
113
|
|
|
function signed(number, maxValue) { |
|
114
|
|
|
if (number > parseInt(maxValue / 2, 10) - 1) { |
|
115
|
|
|
number -= maxValue; |
|
116
|
|
|
} |
|
117
|
|
|
return number; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
module.exports.fromBytes = fromBytes; |
|
121
|
|
|
|